// ==UserScript==
// @name         HuggingFace Mirror文件下载链接提取器v0.1
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  提取HuggingFace Mirror仓库中的文件下载链接
// @author       You
// @match        https://hf-mirror.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hf-mirror.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建悬浮面板
    function createFloatingPanel() {
        const panel = document.createElement('div');
        panel.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            width: 800px;
            height: 600px;
            background: white;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            z-index: 10000;
            display: flex;
            flex-direction: column;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        `;

        // 创建标题栏
        const titleBar = document.createElement('div');
        titleBar.style.cssText = `
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            padding-bottom: 5px;
            border-bottom: 1px solid #eee;
        `;

        const title = document.createElement('div');
        title.textContent = '文件下载链接';
        title.style.fontWeight = 'bold';

        const closeButton = document.createElement('button');
        closeButton.textContent = '×';
        closeButton.style.cssText = `
            border: none;
            background: none;
            font-size: 20px;
            cursor: pointer;
            padding: 0 5px;
        `;
        closeButton.onclick = () => panel.remove();

        titleBar.appendChild(title);
        titleBar.appendChild(closeButton);

        // 创建文本框
        const textarea = document.createElement('textarea');
        textarea.style.cssText = `
            width: 100%;
            height: 100%;
            resize: none;
            border: 1px solid #ddd;
            border-radius: 3px;
            padding: 10px;
            font-family: monospace;
            flex: 1;
        `;

        panel.appendChild(titleBar);
        panel.appendChild(textarea);
        document.body.appendChild(panel);

        return textarea;
    }

    // 提取下载链接
    function extractDownloadLinks() {
        const urls = [];
        const fileInfos = [];
        const fileRows = document.querySelectorAll('li.relative.grid');
        
        fileRows.forEach(row => {
            const downloadLink = row.querySelector('a[download][href]');
            if (downloadLink) {
                const fileName = row.querySelector('.truncate.group-hover\\:underline').textContent.trim();
                const fileSize = row.querySelector('.truncate.max-sm\\:text-xs').textContent.trim();
                
                // 添加URL到URL列表
                urls.push(downloadLink.href);
                
                // 添加文件信息到文件信息列表
                fileInfos.push(`文件名: ${fileName}\n大小: ${fileSize}`);
            }
        });

        // 组合URL列表和文件信息
        return urls.join('\n') + '\n\n' + fileInfos.join('\n\n');
    }

    // 检查页面是否包含文件列表
    function hasFileList() {
        return document.querySelectorAll('li.relative.grid').length > 0;
    }

    // 使用MutationObserver监听DOM变化
    function observeDOM() {
        const observer = new MutationObserver((mutations) => {
            if (hasFileList()) {
                observer.disconnect();
                const textarea = createFloatingPanel();
                textarea.value = extractDownloadLinks();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // 页面加载完成后开始监听
    if (document.readyState === 'loading') {
        window.addEventListener('DOMContentLoaded', observeDOM);
    } else {
        observeDOM();
    }
})(); 